// pages/address-change/index.js
const app = getApp();
const { PageBase } = require('../../mixins/page');
const { chooseLocation, navBack } = require('../../common/platform');
const { getLocationValue, setLocationValue } = require('../../biztools/info');
const { getMemberAddressList } = require('../../api/member');

Page({
    mixins: [PageBase],
    data: {
        addressList: [],
    },
    onLoad: function (options) {

    },
    onShow() {
        this.setData({
            addressValue: getLocationValue().name || '未知地址',
        });
    },
    async getUserAddress() {
        const list = await getMemberAddressList();
        this.setData({
            addressList: list,
        });
    },
    async chooseLocation() {
        const res = await chooseLocation();
        console.log('AddressSelect => 选择的地址为', res);
        setLocationValue({
            name: res.name,
            address: res.address,
            latitude: res.latitude,
            longitude: res.longitude,
        });
        app.globalData.isUserChooseAddress = true;
        this.setData({
            addressValue: res.name,
        });
        navBack();
    },
    handleAddressClick(event) {
        console.log(event);
        const { address } = event.currentTarget.dataset;
        setLocationValue({
            name: address.address,
            address: address.address,
            latitude: address.latitude,
            longitude: address.longitude,
        });
        app.globalData.isUserChooseAddress = true;
        app.globalData.isNeedRefreshAddress = true;
        this.setData({
            addressValue: address.address,
        });
        navBack();
    },
})